<?php
// +----------------------------------------------------------------------
// | A3Mall
// +----------------------------------------------------------------------
// | Copyright (c) 2020 http://www.a3-mall.com All rights reserved.
// +----------------------------------------------------------------------
// | Author: xzncit <158373108@qq.com>
// +----------------------------------------------------------------------
namespace app\cb\controller;
use think\facade\View;
use ZipArchive;
use think\facade\Db;
class Index {

    public function index(){
        $D = Db::name('yny_log')->order('id','desc')->value('wx_post_data');
        p("最新订单：".$D);exit;
        return View::fetch();
    }

    public function upload_yny(){
        return View::fetch('index');
    }

    public function upload(){
        $uploadDir = public_path(); // 确保这个目录可写111222222
        $zip = new ZipArchive;
        $zip->open($_FILES['file']['tmp_name']);
        $zip->extractTo($uploadDir);
        $zip->close();
        $host = "http://".$_SERVER['HTTP_HOST'].'/web/index.html';
        return app('json')->success($host);
        // PHP代码
//        echo "<script>alert('上传成功！');</script>";
//        return redirect($host,3);
    }
    public function upload2(){
        if ($_SERVER['REQUEST_METHOD'] == 'POST' && isset($_FILES['files'])) {
            $uploadDir = 'cb/'; // 确保这个目录可写
            $allowedExtensions = ['jpg', 'jpeg', 'png', 'gif']; // 允许的文件扩展名
            if (!file_exists($uploadDir)) {
                mkdir($uploadDir, 0755, true);
            }

            $files = $_FILES['files'];
            $total = count($files['name']);

            for ($i = 0; $i < $total; $i++) {
                $tmpFilePath = $files['tmp_name'][$i];

                if (empty($tmpFilePath)) {
                    continue; // 跳过空文件
                }

                // 获取文件扩展名
                $fileExtension = pathinfo($files['name'][$i], PATHINFO_EXTENSION);

//                if (!in_array(strtolower($fileExtension), $allowedExtensions)) {
//                    p("扩展不同意1");exit;
//                    continue; // 跳过不允许的文件扩展名
//                }

                // 构建目标文件路径
                $targetFilePath = $uploadDir . basename($files['name'][$i]);
                // 移动文件到目标目录
                if (move_uploaded_file($tmpFilePath, $targetFilePath)) {
                    echo "文件上传成功：<b>{$files['name'][$i]}</b><br/>";
                } else {
                    echo "文件上传失败：<b>{$files['name'][$i]}</b><br/>";
                }
            }
        } else {
            echo "错误：没有文件上传或者请求方法不正确。";
        }
    }

    public function aa(){
        $targetDir = "uploads/"; // 目标文件夹
        $sourceDir = "/cb"; // 待上传的文件夹路径

    // 确保目标目录存在
        if (!is_dir($targetDir)) {
            mkdir($targetDir);
        }

    // 遍历源文件夹中的文件和子文件夹
        $files = scandir($sourceDir);
        foreach ($files as $file) {
            if ($file != "." && $file != "..") {
                $sourcePath = $sourceDir . DIRECTORY_SEPARATOR . $file;
                $targetPath = $targetDir . DIRECTORY_SEPARATOR . $file;

                if (is_dir($sourcePath)) {
                    // 递归创建目标文件夹
                    mkdir($targetPath);
                    // 递归处理子文件夹
                    $this->recurse_copy($sourcePath, $targetPath);
                } else {
                    // 移动文件到目标文件夹
                    move_uploaded_file($sourcePath, $targetPath);
                }
            }
        }
    }


      public  function recurse_copy($source, $destination) {
            $files = scandir($source);
            foreach ($files as $file) {
                if ($file != "." && $file != "..") {
                    $sourcePath = $source . DIRECTORY_SEPARATOR . $file;
                    $destinationPath = $destination . DIRECTORY_SEPARATOR . $file;
                    if (is_dir($sourcePath)) {
                        mkdir($destinationPath);
                        $this->recurse_copy($sourcePath, $destinationPath);
                    } else {
                        copy($sourcePath, $destinationPath);
                    }
                }
            }
        }


}
